/*
 * Monkey-Moore - A simple and powerful relative search tool
 * Copyright (C) 2007 Ricardo J. Ricken (Darkl0rd)
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include "monkey_app.hpp"
#include "monkey_frame.hpp"
#include "constants.hpp"

#include <wx/file.h>

bool MonkeyApp::OnInit ()
{
   if (!wxApp::OnInit())
      return false;

   wxImage::AddHandler(new wxPNGHandler());

   // set up language selection system
   loc.Init(wxLANGUAGE_DEFAULT, wxLOCALE_CONV_ENCODING);
   wxLocale::AddCatalogLookupPathPrefix(wxT("./language"));
   loc.AddCatalog(wxT("monkeymoore"));

   // tries to load the config file, creating a fresh one if it doesn't exist
   if (wxFile::Exists(MM_CONFIG_FILE))
   {
      if (!prefs.load(MM_CONFIG_FILE))
         wxMessageBox(_("Error during preferences loading.\nMonkey-Moore will use the default settings."), _("Error"));
   }
   else prefs.save(MM_CONFIG_FILE, true);

   MonkeyFrame *f = new MonkeyFrame(wxString::Format(wxT("Monkey-Moore %s"), MM_VERSION), prefs, wxDefaultPosition, wxSize(450, 460));
   f->SetBackgroundColour(wxColour(240, 240, 240));
   f->SetMinSize(wxSize(440, 440));

   // restores the saved window properties
   if (!prefs.getBool(wxT("window-centered")) && prefs.getBool(wxT("remember-position")))
      f->SetPosition(prefs.getPoint(wxT("window-position-x"), wxT("window-position-y")));

   if (prefs.getBool(wxT("remember-size")))
      f->SetSize(prefs.getSize(wxT("window-size-w"), wxT("window-size-h")));

   if (prefs.getBool(wxT("window-centered"))) f->Center();
   if (prefs.getBool(wxT("window-maximized"))) f->Maximize();

   f->Show();
   SetTopWindow(f);

   return true;
}

int MonkeyApp::OnExit()
{
   prefs.save(MM_CONFIG_FILE);

   return wxApp::OnExit();
}
